﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SGArchiveSystem7xDB.Linq
{
    public class T_UserDal
    {
        static SGArchiveSystem7xEntities conn;
        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="en"></param>
        /// <returns></returns>
        public static _ReturnValue Add_User(T_User en)
        {
            try
            {
                conn = new SGArchiveSystem7xEntities();
                ///同一个PID里的不能有名字重复的模块
                if (conn.T_User.Select(c => c).Where(c => c.U_NAME == en.U_NAME).Count() > 0)
                    return _ReturnValue.Already;
                else
                {
                    conn.T_User.Add(en);
                    conn.SaveChanges();
                    return _ReturnValue.OK;
                }
            }
            catch (Exception ex) { return _ReturnValue.Err; }
        }
        /// <summary>
        /// 修改用户
        /// </summary>
        /// <param name="en"></param>
        /// <returns></returns>
        public static _ReturnValue Modify_User(T_User en)
        {
            try
            {
                conn = new SGArchiveSystem7xEntities();
                ///不能有名字重复的,并且ID号不相同
                if (conn.T_User.Select(c => c).Where(c => c.U_NAME == en.U_NAME && c.U_ID != en.U_ID).Count() > 0)
                    return _ReturnValue.Already;
                else
                {
                    var query = conn.T_User.Select(c => c).Where(c => c.U_ID == en.U_ID);
                    foreach (var n in query)
                    {
                        en.U_D01 = n.U_D01;
                        en.U_D02 = n.U_D02;
                        //en.U_V01 = n.U_V01;
                        en.U_PWD = n.U_PWD;
                        if (!SGArchiveSystem7xCommons.SGEntity.ModifyEntity(n, en, "U_ID"))
                            return _ReturnValue.Err;
                    }
                    conn.SaveChanges();
                    return _ReturnValue.OK;
                }
            }
            catch (Exception ex) { return _ReturnValue.Err; }
        }
        /// <summary>
        /// 修改密码(需要输入旧密码的)
        /// </summary>
        /// <param name="id"></param>
        /// <param name="pwd">新的密码</param>
        /// <returns></returns>
        public static _ReturnValue Modify_Pwd(int id, string oldpwd, string pwd)
        {
            try
            {
                oldpwd = SGArchiveSystem7xCommons.PWD_MD5.MD5Encrypt(oldpwd);
                pwd = SGArchiveSystem7xCommons.PWD_MD5.MD5Encrypt(pwd);

                conn = new SGArchiveSystem7xEntities();
                var query = conn.T_User.Select(c => c).Where(c => c.U_ID == id && c.U_PWD == oldpwd);
                if (query.Count() == 0)
                    return _ReturnValue.Already;
                foreach (var n in query)
                {
                    n.U_PWD =  pwd;
                    n.U_D02 = DateTime.Now;
                }
                conn.SaveChanges();

                return _ReturnValue.OK;
            }
            catch (Exception ex) { return _ReturnValue.Err; }
        }

        /// <summary>
        /// 修改密码，不需要旧密码
        /// </summary>
        /// <param name="id"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public static _ReturnValue Modify_Pwd(int id, string pwd)
        {
            try
            {
                pwd = SGArchiveSystem7xCommons.PWD_MD5.MD5Encrypt(pwd);

                conn = new SGArchiveSystem7xEntities();
                var query = conn.T_User.Select(c => c).Where(c => c.U_ID == id);
                if (query.Count() == 0)
                    return _ReturnValue.Already;
                foreach (var n in query)
                {
                    n.U_PWD = pwd;
                    n.U_D02 = DateTime.Now;
                }
                conn.SaveChanges();
                return _ReturnValue.OK;
            }
            catch (Exception ex) { return _ReturnValue.Err; }
        }

        public static V_User GetLoginUser(string username, string pwd)
        {
            try
            {
                conn = new SGArchiveSystem7xEntities();
                var query = conn.V_User.Select(c => c).Where(c => c.U_NAME == username);
                if (pwd != "" && pwd != null)
                    query.Where(c => c.U_PWD == pwd);
                return query.Single();
                //return conn.V_User.Select(c => c).Where(c => c.U_NAME == username&&c.U_PWD== pwd).Single();
            }
            catch { return null; }
        }
        public static List<V_User> GetUser(string rname, int dep, int role, out int count, int status = 1, int start = -1, int row = -1)
        {
            count = 0;
            try
            {
                conn = new SGArchiveSystem7xEntities();
                var query = conn.V_User.Select(c => c);
                if (status > -1)
                    query = query.Where(c => c.U_STATUS == status);
                if (rname != null && rname != "")
                    query = query.Where(c => c.U_RNAME.Contains(rname));
                if (dep > -1)
                    query = query.Where(c => c.U_DEPID == dep);//还不知道哪个是部门名，暂用v05代替
                if (role > -1)
                    query = query.Where(c => c.U_ROLEID == role);
                if (start != -1 && row != -1)
                {
                    count = query.Count();
                    return query.OrderBy(c => c.U_ID).Skip(start).Take(row).ToList();
                }
                else
                {
                    var list = query.ToList();
                    count = list.Count;
                    return list;
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public static T_User Get_UserId(int id)
        {
            try
            {
                conn = new SGArchiveSystem7xEntities();
                return conn.T_User.Select(c => c).Where(c => c.U_ID == id).Single();
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        public static List<V_User> GET_EMAIL(int? depid, int i01)
        {
            try
            {
                conn = new SGArchiveSystem7xEntities();
                return conn.V_User.Select(c => c).Where(c => c.U_DEPID == depid&&c.U_I01==i01).ToList();
            }
            catch (Exception ex)
            {
                return null;
            }
        }
    }
}
